/**
 * 
 */
package edu.lnu.project.timeeditapp.adapter;

import java.util.ArrayList;
import java.util.TreeSet;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lnu.project.timeeditapp.R;

import edu.lnu.project.timeeditapp.EventDialog.EventDetailedDialog;
import edu.lnu.project.timeeditapp.db.TimeEditDataSource;
import edu.lnu.project.timeeditapp.helper.MyHelper;
import edu.lnu.project.timeeditapp.parser.Event;

/**
 * @author Libra
 *
 */
public class MyListViewAdapter extends BaseAdapter {

	private static final int TYPE_EVENT = 0;
	private static final int TYPE_DAY = 1;
	private static final int TYPE_MAX_COUNT = TYPE_DAY + 1;

	private ArrayList<Event> mData = new ArrayList<Event>();
	private LayoutInflater mInflater;
	private Activity mActivity;
	TimeEditDataSource datasource;
	//Set to keep track of which items are day items
	private TreeSet<Integer> mDaysSet = new TreeSet<Integer>();

	/**
	 * Constructor
	 * @param inflater
	 * @param activity
	 */
	public MyListViewAdapter(LayoutInflater inflater, Activity activity) {
		this.mInflater = inflater;
		this.mActivity = activity;
		MyHelper.initializeSharedPreferences(activity);
	}

	/**
	 * Adds list item that specifies which event it is
	 * @param event
	 */
	public void addEventItem(final Event event) {
		if(MyHelper.visibilityColorPreferences.getBoolean(event.getCourse()+MyHelper.VISIBILITY, true)){
			mData.add(event);
			notifyDataSetChanged();
		}
	}

	/**
	 * Adds list item that specifies which day it is
	 * @param event
	 */
	public void addDayItem(final Event event) {
		if(MyHelper.visibilityColorPreferences.getBoolean(event.getCourse()+MyHelper.VISIBILITY, true)){
			mData.add(event);
			// save day position
			mDaysSet.add(mData.size() - 1);
			notifyDataSetChanged();
		}
	}


	@Override
	public int getItemViewType(int position) {
		return mDaysSet.contains(position) ? TYPE_DAY: TYPE_EVENT;
	}

	/**
	 * Removes all data in the adapter
	 */
	public void clear(){
		try{
			mData.clear();
			for (int i = 0; i<mData.size(); i++) {
				mData.remove(i);
			}
			mDaysSet.clear();
			notifyDataSetChanged();
		} catch ( Exception e ){
			System.out.println("Error unable to clear adapter: "+e);
		}
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Event getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		ArrayList<Object> tagObjects = new ArrayList<Object>();
		//get the event data from the database in order to correct state of reminder checked in the dialog once it's clicked.
		datasource = new TimeEditDataSource(mActivity);
		datasource.open();
		final Event event = datasource.getEvent((mData.get(position).getId()));
		datasource.close();
		int type = getItemViewType(position);

		// If convertView is null create a new
		if (convertView == null) {
			//Create new ViewHolder to the new convertView
			holder = new ViewHolder();

			switch (type) {
			case TYPE_EVENT:

				convertView = mInflater.inflate(R.layout.listview_item_event, null); 

				//Connect the ViewHolder with the views in the layout
				holder.room = (TextView)convertView.findViewById(R.id.textViewListViewItem_room);
				holder.time = (TextView)convertView.findViewById(R.id.textViewListViewItem_time); 
				holder.course = (TextView)convertView.findViewById(R.id.textViewListViewItem_course);
				holder.layout = (LinearLayout)convertView.findViewById(R.id.layoutListView);

				break;
			case TYPE_DAY:

				//Chose the layout for the day item
				convertView = mInflater.inflate(R.layout.listview_item_day, null);

				//Connect the ViewHolder with the views in the layout
				holder.day = (TextView)convertView.findViewById(R.id.textViewListViewItem_day);
				holder.date = (TextView)convertView.findViewById(R.id.textViewListViewItem_date);

				break;
			}
			tagObjects.add(holder);
			tagObjects.add(event.getId());
			convertView.setTag(tagObjects);
		} 
		else {
			holder = (ViewHolder) ((ArrayList<Object>)convertView.getTag()).get(0);
		}
		switch (type) {
		case TYPE_EVENT:

			//Store information that should be shown in the ViewHolder
			holder.room.setText(event.getRoom());
			holder.time.setText(event.getBeginTime()+"-"+event.getEndTime());
			holder.course.setText(event.getCourse());
			holder.layout.setBackgroundColor(mActivity.getResources().
					getColor(MyHelper.visibilityColorPreferences.
							getInt(event.getCourse()+MyHelper.COLOR, R.color.Red)));
			break;
		case TYPE_DAY:

			//Store information that should be shown in the ViewHolder
			holder.day.setText(event.getWeekday());
			holder.date.setText(event.getBeginDate());
			break;
		}

		//Set click listener to the events in the listView
		convertView.setOnClickListener( new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(!MyHelper.isRedDay(event)){
					EventDetailedDialog eventDetailedDialog = new EventDetailedDialog(mActivity, event);
					eventDetailedDialog.show();
				}
			}

		});
		return convertView;
	}
}
